/**
 * @version 1.0
 * @author Ha Giang
 * @date 
 */
package Communications;

import Configuration.Utils;
import java.util.*;

/**
 * 
 * This class handles communications in 2 multi-cast channels: Service channel and Client channel
 */
public class Multicast {

    private String mul_addr = Utils.MULTICAST_ADDRESS;//multicast address
    private int client_channel = Utils.MULTICAST_CLIENT_CHANNEL;//port multicast for client
    private int service_channel = Utils.MULTICAST_SERVICE_CHANNEL;//port multicast for services
    MulticastByChannel service_mul, client_mul;
    private Queue clientChannelAnnouncement = null;
    private Queue serviceChannelAnnouncement = null;

    /**
     * Constructor, no parameter
     */
    public Multicast() {
    }

    /**
     * Start a multi-cast channel for clients 
     * @return A queue that consists a list of announcement sent by clients in this channel
     */
    public Queue startMulticastClientChannel() {
        try {
            System.out.println("Start Client Multicast Channel....");
            client_mul = new MulticastByChannel(mul_addr, client_channel);
            this.clientChannelAnnouncement = client_mul.getAnnouncement();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return this.clientChannelAnnouncement;
        }
    }

    /**
     * Start a multi-cast service for clients
     * @return A queue that consists a list of announcement sent by services in this channel
     */
    public Queue startMulticastServiceChannel() {
        try {
            System.out.println("Start Service Multicast Channel...");
            service_mul = new MulticastByChannel(mul_addr, service_channel);
            this.serviceChannelAnnouncement = service_mul.getAnnouncement();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return this.serviceChannelAnnouncement;
        }
    }

    /**
     * Announce a message to Client Channel
     * @param msg The message to announce
     */
    public void announceMsgToClientChannel(String msg) {
        client_mul.announceMsg(msg);
    }

    /**
     * Announce a message to Service Channel
     * @param msg The message to announce
     */
    public void announceMsgToServiceChannel(String msg) {
        service_mul.announceMsg(msg);
    }

    /**
     * Close multi-cast Service Channel
     */
    public void closeMulticastServiceChannel() {
        System.out.println("Closing Service Channel...");
        service_mul.closeChannel();
    }

    /**
     * Close multi-cast Client channel
     */
    public void closeMulticastClientChannel() {
        System.out.println("Closing Client Channel...");
        client_mul.closeChannel();
    }
}
